﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using SqlRunner.uiStatus;
using SQLRunnerLib.Services;

namespace SqlRunner
{
    public partial class frmEnvironmentDetails
    {
        private EnvFormStatus applicationStatus = EnvFormStatus.Initial;
        private readonly Groups<EnvFormStatus> applicationGroups = new Groups<EnvFormStatus>();

        private void SetGroups()
        {
            EnvFormStatus flag = EnvFormStatus.EnvNameIsAvailable |
                                 EnvFormStatus.ServerNameIsAvailable |
                                 EnvFormStatus.ConnIsAvailable |
                                 EnvFormStatus.DbWasSelected;

            var g = CreateGroup(flag, new List<Component> {grpSettings});      
            applicationGroups.AddGroup(g);

            flag -= EnvFormStatus.EnvNameIsAvailable;
            g = CreateGroup(flag, new List<Component> { cboAuthentication, chkRemeberPassword, btnConnect });      
            applicationGroups.AddGroup(g);

            flag -= EnvFormStatus.ServerNameIsAvailable;
            g = CreateGroup(flag, new List<Component> { cboDatabase });
            applicationGroups.AddGroup(g);

            flag -= EnvFormStatus.ConnIsAvailable;
            g = CreateGroup(flag, new List<Component> { btnOk });
            applicationGroups.AddGroup(g);

            ApplicationStatus = applicationStatus;
        }

        private GroupControls<EnvFormStatus> CreateGroup( EnvFormStatus flag, 
                                                          IEnumerable<Component> list)
        {
            var g = new GroupControls<EnvFormStatus>("Enabled", flag);
            foreach (var component in list)
            {
                g.AddControl(component);
            }
            return g;
        }

        private EnvFormStatus ApplicationStatus
        {
            set
            {
                applicationStatus = value;
                applicationGroups.UpdateStatus(value);
            }
        }

        private void txtEnvironment_TextChanged(object sender, EventArgs e)
        {
            CheckFormStatus();
        }

        private void cboServer_TextChanged(object sender, EventArgs e)
        {
            CheckFormStatus();
        }

        private void CheckFormStatus()
        {
            if (txtEnvironment.Text.Length == 0)
            {
                ApplicationStatus = EnvFormStatus.Initial;
                return;
            }
            if (cboServer.Text.Length == 0)
            {
                ApplicationStatus = EnvFormStatus.EnvNameIsAvailable;
                return;
            }
            if (!IsConnAvailable)
            {
                ApplicationStatus = EnvFormStatus.ServerNameIsAvailable;
                return;
            }
            if (cboDatabase.Text.Length == 0)
            {
                ApplicationStatus = EnvFormStatus.ConnIsAvailable;
                return;
            }
            ApplicationStatus = EnvFormStatus.DbWasSelected;
        }

        private void cboDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckFormStatus();
        }

        private void cboServer_Leave(object sender, EventArgs e)
        {
            if (!IsConnAvailable) return;
            if (ServiceContainer.SQLConnection.ServerName.Equals(cboServer.Text)) return;
            IsConnAvailable = false;
            CheckFormStatus();
        } 
    }
}
